package com.jl.utils.niuniu;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @Auther: zhouhao
 * @Date: 2020/5/7
 * @Description:   一副新牌
 */
@Data
@NoArgsConstructor
@Slf4j
public class GlobalData {

    private static String[] suit = {"1","2","3","4"};

    private static String[] face = {"1","2","3","4","5","6","7","8","9","10","11","12","13"};

    private int cardNum = 52;

    private static int remain = 0;

    private String[] globalData;

    private BlockingQueue<String> queue = new ArrayBlockingQueue<>(100);

    public int getCardNum() {
        return cardNum;
    }

    /**
     *
     * @param type 1为无癞子， 2 为有癞子
     */
    public GlobalData(int type){
        if(type ==2){ //有癞子
            cardNum =54;
            globalData = new String[cardNum];
            int i = 0;
            for (int k = 0; k < 4; k++) {
                for (int j = 0; j < 13; j++) {
                    globalData[i++] = suit[k] + "-" + face[j];
                }
            }
            globalData[52] = "king-small";
            globalData[53] = "king-big";
        }else if(type==1){ //无癞子
            globalData = new String[cardNum];
            int i = 0;
            for (int k = 0; k < 4; k++) {
                for (int j = 0; j < 13; j++) {
                    globalData[i++] = suit[k] + "-" + face[j];
                }
            }
        }else if (type==3){ //无癞子无花牌
            cardNum = 40;
            globalData = new String[cardNum];
            int i = 0;
            for (int k = 0; k < 4; k++) {
                for (int j = 0; j < 10; j++) {
                    globalData[i++] = suit[k] + "-" + face[j];
                }
            }
        }
        shuffleCard();
    }

    /**
     * 洗牌
     */
    public void shuffleCard(){
        Random rd = new Random();
        for(int i=0;i<cardNum;i++){
            int j = rd.nextInt(cardNum);//生成随机数
            String temp = globalData[i];//交换
            globalData[i]=globalData[j];
            globalData[j]=temp;
        }
        List<String> strings = Arrays.asList(globalData);
        log.info("牌库一共{}张牌",strings.size());
        queue.addAll(strings);
    }


    /*
    生成一张牌
     */
    public Card getCard(){
        String poll = queue.poll();
        String[] s = poll.split("-");
        Card card = new Card();
        card.setCard(poll);
        card.setSuitType(s[0]);
        card.setFaceType(s[1]);
        return card;
    }

    /*
    生成N张牌
     */
    public List<Card> getCard(int numb){
        List<Card> list = new ArrayList<>();
        for (int i = 0; i < numb; i++) {
            Card card = getCard();
            list.add(card);
        }
        return list;
    }
}
